package com.xinhe.cn.cpbox.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xinhe.cn.cpbox.entity.po.ScripSend;
import com.xinhe.cn.cpbox.entity.vo.GetScriptVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 * <p>
 * 已发送的纸条 Mapper 接口
 * </p>
 *
 * @author LHB
 * @since 2021-10-09
 */
@Repository
public interface ScripSendMapper extends BaseMapper<ScripSend> {
    @Select("select 1 from scrip_send where send_id=#{sendId} and type=#{type} limit 1")
    Integer findCanFreeGet(@Param("type") String type, @Param("sendId") String sendId);

    @Select("select 1 from scrip_send where send_id<>#{getUserId} and type=#{type} and checked=1 and deleted=0 " +
            "and scrip_id not in (select scrip_id from scrip_get where type=#{type} and get_user_id=#{getUserId}) limit 1")
    Integer findCanGetScrip(@Param("type") String type, @Param("getUserId") String getUserId);

    @Select("select scrip_id from scrip_send where send_id<>#{getUserId} and type=#{type} and checked=1 and deleted=0 " +
            "and scrip_id not in (select scrip_id from scrip_get where type=#{type} and get_user_id=#{getUserId}) order by rand() limit 1")
    Long getRandomScrip(String type, String getUserId);

    @Update("update scrip_send set get_num=get_num+#{num} where scrip_id=#{scripId}")
    void addGetNum(@Param("scripId") Long scripId, @Param("num") int num);
}
